package cn.chiship.framework.common.util;

import cn.chiship.framework.common.pojo.dto.SystemExceptionLogDto;
import cn.chiship.framework.common.config.properties.GlobalProperties;
import cn.chiship.framework.common.pojo.dto.SystemOptionLogDto;
import cn.chiship.sdk.core.exception.custom.BusinessException;
import cn.chiship.sdk.core.util.DateUtils;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.util.SpringContextUtil;
import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lj
 */
public class FrameworkUtil2 {
    private static final Logger logger = LoggerFactory.getLogger(FrameworkUtil2.class);

    private FrameworkUtil2() {
    }

    public static void formatErrorMessage(SystemExceptionLogDto systemExceptionLogDto) {
        logger.error("=================开始=====================");
        if (!StringUtil.isNullOrEmpty(systemExceptionLogDto.getUserName())) {
            logger.error("【操作人员】{}", systemExceptionLogDto.getUserName() + "(" + systemExceptionLogDto.getRealName() + ")");
        }
        logger.error("【操作时间】{}", DateUtils.getTime());
        logger.error("【请求码】{}", systemExceptionLogDto.getRequestId());
        logger.error("【错误描述】{}", systemExceptionLogDto.getDescription());
        logger.error("【请求接口】{}", systemExceptionLogDto.getBasePath() + systemExceptionLogDto.getUri());
        logger.error("【传入的参数】{}", systemExceptionLogDto.getParameter());
        logger.error("【错误栈描述】{}", systemExceptionLogDto.getErrorLocalizedMessage());
        logger.error("=================结束=====================");
    }

    public static void formatInfoMessage(SystemOptionLogDto systemOptionLogDto) {
        logger.info("=================开始=====================");
        if (!StringUtil.isNullOrEmpty(systemOptionLogDto.getUserName())) {
            logger.info("【操作人员】{}", systemOptionLogDto.getUserName() + "(" + systemOptionLogDto.getRealName() + ")");
        }
        logger.info("【操作时间】{}", DateUtils.dateTime(systemOptionLogDto.getStartTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        logger.info("【花费时间】{}ms", systemOptionLogDto.getSpendTime());
        logger.info("【操作名称】{}", systemOptionLogDto.getDescription());
        logger.info("【函数名称】{}", systemOptionLogDto.getUri());
        logger.info("【传入的参数】{}", systemOptionLogDto.getParameter());
        logger.info("【返回的结果】{}", systemOptionLogDto.getResult());
        logger.info("=================结束=====================");
    }

    public static GlobalProperties getGlobalProperties() {
        return SpringContextUtil.getBean(GlobalProperties.class);
    }

    public static void generatorDruidPassword(String password) {
        try {
            ConfigTools.main(new String[]{password});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<JSONObject> assemblyDataTree(String pid, List records) {
        return assemblyDataTree("pid", pid, records);
    }

    public static List<JSONObject> assemblyDataTree(String pidFiled, String pid, List records) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Object record : records) {
            Class<?> clazz = record.getClass();
            Field field = null;
            try {
                field = clazz.getDeclaredField(pidFiled);
            } catch (NoSuchFieldException e) {
                throw new BusinessException("没有属性：" + pidFiled);
            }
            if (ObjectUtils.isEmpty(field)) {
                return null;
            }
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(record);
            } catch (IllegalAccessException e) {
                throw new BusinessException("不兼容的数据类型");
            }
            if (ObjectUtils.isEmpty(value)) {
                return null;
            }

            if (pid.equals(value)) {
                jsonObjects.add(JSON.parseObject(JSON.toJSONString(record)));
            }
        }
        for (JSONObject jsonObject : jsonObjects) {
            List<JSONObject> children = assemblyDataTree(pidFiled, jsonObject.getString("id"), records);
            if (!children.isEmpty()) {
                jsonObject.put("children", children);
            }
        }
        return jsonObjects;
    }
}
